Microsoft Visual C++ 6.0 sample
By using Automation, the following samples, written in Visual C++,
demonstrates how to:
- Create an object of FindGraph.
- Use IDispatch for embedded application.
- Change properties.
- Add series of points.
- Get all points selected.
- Perform error checking.
Project and testVC.exe see in directory TestVC.
This runs program FindGraph as embedded.
How to use Crov.exe sample:
- Run Crov.exe;
- Push button Insert FindGraph
or select menu item <Edit><Insert FindGraph>
- FindGraph plot will appear as embedded object;
- Deactivate it,
to do this click on Crov work area;
- Message Box will appear:
Document was not saved. Save changes?
Select to save;
- Activate FindGraph embedded object,
to do this click once on FindGraph object;
- Toolbar FindGraph buttons will become enabled;
- Push button Add new points
or select menu item <FindGraph><Add points>;
- Two new points series will appear.
///////////////////////////////////////////////////////////////
// How to realise automation FindGraph.
// 1. Create IFindGraph wrapper class from a type FindGraph.tlb
// 2. Add #include "FindGraph.h"
//
// 3. Add to CRectItem:
// LPDISPATCH GetIDispatch();
// This returns IDispatch* for embedded application
// see sample from MSDN: HOWTO: IQ: Q184663 for details
//
// 4. Call FindGraphs's methods as wrapper:
// DECLARE_FINDGRAPHS(FindGraph);
// FindGraph.ReleaseDispatch();
//
//
//
//
//
//
///////////////////////////////////////////////////////////////
// nColor - color number
// 0 BLACK
// 1 BLUE
// 2 GREEN
// 3 RED
// 4 BROWN
// 5 GRAY
//
// nShape - marker
// 0 RECT (CROSS)
// 1 TRIA
// 2 CIRC
//
// nWidth - X radius, 0,1 mm
// fAspect - aspect ratio Y/X
///////////////////////////////////////////////////////////////
// IDispatch* for embedded application
//
LPDISPATCH CMainView::GetIDispatch()
{
if (m_pSelection != NULL)
return m_pSelection->GetIDispatch();
return NULL;
}
//start define--------------------------
#define DECLARE_FINDGRAPHS(FindGraphs) \
if (m_pSelection==NULL) return;\
m_pSelection->DoVerb(OLEIVERB_SHOW, this);\
LPDISPATCH lpDisp = GetIDispatch();\
if (lpDisp == NULL) return;\
IFindGraph FindGraphs;\
FindGraphs.AttachDispatch(lpDisp);\
//----------------------------end define
// How to add at some points to new series
//
void CMainView::FindGraphsSet()
{
DECLARE_FINDGRAPHS(FindGraph);
CString str;
long dwId;
short nColor = 1,
nShape = 2,
nWidth =25;
double fAspect = 1.;
CString strName = _T("By one");
// Create new series
dwId = FindGraph.DotsNew(nColor, nShape, nWidth, fAspect, strName);
m_dwIdDots = dwId;
if (dwId >= 0)
{
// How to add single point to series with dwId
int n = 20;
for (int i=0; i<n; i++)
{
double fX = 1. + 0.25*i,
fY = 7. - 0.35*i,
fZ = i;
FindGraph.DotsAddPoint(dwId, fX, fY, fZ);
// Optionally: paint appearance of each point
FindGraph.DotsUpdate(dwId);
}
}
// To add at once some points
// Create new series
// dwId - identifier of a series
dwId = FindGraph.DotsNew(2, 1, 20, 1, _T("At once"));
m_dwIdDots = dwId;
if (dwId >= 0)
{
int n = 1000;
int nParam = 3;
// If nParam==3 (x, y, z), if nParam==2 - x, y only
// One dimensional array with n*nParam elements zero-based indexing
SAFEARRAYBOUND bounds[1];
bounds[0].cElements = n*nParam;
bounds[0].lLbound = 0;
// Allocate the memory for the descriptor and the array data
SAFEARRAY *psa = SafeArrayCreate(VT_R8, 1, bounds);
if (psa)
{
psa->fFeatures = FADF_AUTO|FADF_FIXEDSIZE;
double *pr;
SafeArrayAccessData(psa, (void**)&pr);
// Initialize each element
int ip=0;
for (int i = 0; i < n; i++)
{
double fi = 2.*3.14159/n*i;
double fX = 4.+3.*sin(fi),
fY = 3.+2.*cos(fi),
fZ = i;
pr[ip++] = fX;
pr[ip++] = fY;
pr[ip++] = fZ;
}
SafeArrayUnaccessData(psa);// release lock on array state
// Create a VARIANT that contains our safearray
VARIANT var; ZeroMemory(&var, sizeof(var));
var.vt = VT_UI1 | VT_ARRAY;
var.parray = psa;
FindGraph.DotsAddPoints(dwId, nParam, var);
}
FindGraph.DotsUpdate(dwId);
}
FindGraph.ReleaseDispatch();
}
// How to get points selected
//
void CMainView::FindGraphsGet()
{
DECLARE_FINDGRAPHS(FindGraph);
// Copy selected points (X, Y, Z) and put it on the buffer.
long n = FindGraph.SelectedGetStart(0);
if (n)
{
if (n<10)
{
CDot dot;
// In cycle we choose points:
while (FindGraph.SelectedGetDot(&dot.m_fX, &dot.m_fY, &dot.m_fZ)==S_OK)
{
// There can be your operatings with points here
// For example to transfer them to Excel sheet
TRACE("x=%f y=%f z=%f\n", dot.m_fX, dot.m_fY, dot.m_fZ);
}
}
// How to get whole array of selected points at once
else
{
int nParam = 3; // x, y, z
SAFEARRAYBOUND bounds[1];
bounds[0].cElements = n*nParam;
bounds[0].lLbound = 0;
// Allocate the memory for the descriptor and the array data
SAFEARRAY *psa = SafeArrayCreate(VT_R8, 1, bounds);
if (psa)
{
psa->fFeatures = FADF_FIXEDSIZE; // NOTE: not FADF_AUTO
// Create a VARIANT that contains our safearray
VARIANT var; ZeroMemory(&var, sizeof(var));
var.vt = VT_UI1 | VT_ARRAY;
var.parray = psa;
// Fill array with points (X, Y, Z).
if (FindGraph.SelectedGetDots(nParam, &var)==S_OK)
{
double *pr;
SafeArrayAccessData(psa, (void**)&pr);
int ip=0;
// If nParam==3 Points by triples (X, Y, Z)
// If nParam==2 Points by couples (X, Y)
for (int i = 0; i < n; i++)
{
double fX = pr[ip ],
fY = pr[ip+1],
fZ = (nParam==3) ? pr[ip+2] : 0;
ip+=nParam;
// There can be your operatings with points here
TRACE("x=%f y=%f\n",fX, fY);
}
// Release lock on array state
SafeArrayUnaccessData(psa);
}
}
// Free buffer memory
n = FindGraph.SelectedGetStop(0);
}
}
FindGraph.ReleaseDispatch();
}
// The example how to change plot properties
//
void CMainView::FindGraphsParams()
{
// The identifier of a series
DWORD dwId = m_dwIdDots;
DECLARE_FINDGRAPHS(FindGraph);
// Item (FindGraph plot) size, 0,01 mm
CSize size (12000, 8000);
// Set new size to item
SelectionSetSize(size);
FindGraph.SetDocWidth (size.cx/10); // 0,1 mm
FindGraph.SetDocHeight(size.cy/10);
FindGraph.SetDocTitle (_T("From Crov title"));
FindGraph.SetDocComment(_T("From Crov comment"));
FindGraph.SetAxeXname (_T("Xname"));
FindGraph.SetAxeXunit (_T("Xunit"));
FindGraph.SetAxeXstart(-5.); // In X units
FindGraph.SetAxeXscale( 2.); // Ratio: X units / sm
FindGraph.SetAxeYname (_T("Yname"));
FindGraph.SetAxeYunit (_T("Yunit"));
FindGraph.SetAxeYstart(-2.); // In Y units
FindGraph.SetAxeYscale( 2.); // Ratio: Y units / sm
if (dwId >= 0)
{
CString str;
short nColor = 1,
nShape = 2,
nWidth =25;
double fAspect = 1.;
// Get color number of points in series
short nColorGet=0;
if (FindGraph.DotsColorNumGet(dwId, &nColorGet)==S_OK)
{
str.Format("nColorGet=%d",nColorGet);
//AfxMessageBox(str);
}
nColor=nColorGet+1;
// Set new color number
FindGraph.DotsColorNumSet(dwId, nColor);
// Get number of marker
short nShapeGet=0;
if (FindGraph.DotsShapeGet(dwId, &nShapeGet)==S_OK)
{
//str.Format("nShapeGet=%d",nShapeGet);
//AfxMessageBox(str);
}
nShape=nShapeGet+1;
// Set new number of marker
FindGraph.DotsShapeSet(dwId, nShape);
// Get radius of circle point
short nWidthGet=10;
if (FindGraph.DotsWidthGet(dwId, &nWidthGet)==S_OK)
{
//str.Format("nWidthGet=%d",nWidthGet);
//AfxMessageBox(str);
}
nWidthGet*=2;
if (nWidthGet > 50)
nWidthGet = 5;
// Set new radius of circle point
FindGraph.DotsWidthSet(dwId, nWidthGet);
// Get aspect ratio: (Height Y) / (Width X);
double fAspectGet=1;
if (FindGraph.DotsAspectGet(dwId, &fAspectGet)==S_OK)
{
//str.Format("fAspectGet=%f",fAspectGet);
//AfxMessageBox(str);
}
fAspectGet*=2;
if (fAspectGet > 10.)
fAspectGet = 0.25;
// Set new aspect ratio
FindGraph.DotsAspectSet(dwId, fAspectGet);
// Get title of a series
CString strNameGet;
//_bstr_t bstr;
BSTR bstr = strNameGet.AllocSysString();
if (FindGraph.DotsNameGet(dwId, &bstr)==S_OK)
{
strNameGet = CString(bstr);
//AfxMessageBox(strNameGet);
}
strNameGet = "new name";
// Set new title of a series
FindGraph.DotsNameSet(dwId, strNameGet);
// Assigns to connect points with line
FindGraph.DotsShowLine(dwId, 1);
// Next samples:
// How to get RGB value for number of color
// long color = FindGraph.DotsColor(1);
}
FindGraph.DocUpdate();
FindGraph.ReleaseDispatch();
}
See documentation on methods and properties.
รก